package org.itboys.core.service.mongo.web;


import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.core.entity.mongo.SundayAdvertisement;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告信息_service
 */
@Service
public class SundayAdvertisementService extends BaseMongoService<SundayAdvertisement> {
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayUploadService uploadFileService;

    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayAdvertisement> getEntityClass() {
        return SundayAdvertisement.class;
    }

    /**
     * 修改或新增
     * @param advertisement
     * @param mageFile
     * @param videoFile
     * @throws Exception
     */
    public void saveAdvertisement(SundayAdvertisement advertisement,MultipartFile mageFile,MultipartFile videoFile) throws  Exception{
    //保存主图
        Map<String, String> map = uploadFileService.uploadSingle(mageFile);
        if(StringUtils.isNotEmpty(map.get(uploadFileService.UPLOAD_HALF_PATH))){

            advertisement.setImage(map.get(uploadFileService.UPLOAD_HALF_PATH));
        }
        //保存视频
        Map<String, String> map2 = uploadFileService.uploadSingle(videoFile);
        if(StringUtils.isNotEmpty(map2.get(uploadFileService.UPLOAD_HALF_PATH))){
            advertisement.setVideo(map2.get(uploadFileService.UPLOAD_HALF_PATH));
        }

        if(advertisement.getId()==0l){
            super.save(advertisement);
        }else{
            super.updateExceptEmpty(advertisement.getId(),advertisement);
        }
    }

    /**
     * 分页获取广告信息
     * @param request
     * @return
     */
    public PageResult<SundayAdvertisement> select(HttpServletRequest request){
        Map<String,Object> param =ParamUtil.packageMongoExactParam(request,"zoneKey");
        param.put("isDeleted", 0);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"key","name");
        PageResult<SundayAdvertisement> result=super.containpageQuery(request,containsparam,param);
        return result;
    }

    /**
     * 单个查询广告
     * @param id
     * @param zoneKey
     * @return
     */
    public SundayAdvertisement findOne(Long id,String zoneKey){
        SundayAdvertisement advertisement = null;

        if(LongUtil.isNotZreo(id)){

            advertisement=super.getById(id);
            advertisement.setImage(advertisement.getImage());
            advertisement.setVideo(advertisement.getVideo());
            advertisement.setImage(resourceHolder.getStringValue("imgRoot")+advertisement.getImage());
        }else{
            advertisement=new SundayAdvertisement();
            advertisement.setZoneKey(zoneKey);
            //默认广告不转跳
            //advertisement.setPageType(0);
        }
        return  advertisement;
    }

}
